package com.gomcarter.frameworks.mybatis.factory;

import com.gomcarter.frameworks.mybatis.datasource.ReadWriteDataSource;
import lombok.Data;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.lang.reflect.Field;
import java.util.Properties;

@Data
@ConfigurationProperties(value = "yfcloud.frameworks.datasource")
public class ReadWriteDataSourceFactory implements FactoryBean<ReadWriteDataSource>, InitializingBean, DisposableBean {
    private ReadWriteDataSource readWriteDataSource = new ReadWriteDataSource();
    private String writeJdbcUrl;
    private String writeJdbcUser;
    private String writeJdbcPassword;
    private String writeJdbcInitialSize;
    private String writeJdbcMaxActive;
    private String writeJdbcMinIdle;
    private String writeJdbcMaxWait;
    private String writeMinEvictableIdleTimeMillis;
    private String writeValidationQuery;
    private String writeJdbcTestWhileIdle;
    private String writeJdbcTestOnBorrow;
    private String writeJdbcTestOnReturn;
    private String writeJdbcTimeBetweenEvictionRunsMillis;
    private String writeJdbcMinEvictableIdleTimeMillis;
    private String writeJdbcRemoveAbandoned;
    private String writeJdbcRemoveAbandonedTimeout;
    private String writeJdbcLogAbandoned;
    private String writeJdbcFilters;
    private String writeJdbcConnectionInitSqls;
    private String writeJdbcValidationQuery;
    private String readJdbcUrl;
    private String readJdbcUser;
    private String readJdbcPassword;
    private String readeJdbcInitialSize;
    private String readeJdbcMaxActive;
    private String readeJdbcMinIdle;
    private String readeJdbcMaxWait;
    private String readMinEvictableIdleTimeMillis;
    private String readValidationQuery;
    private String readJdbcTestWhileIdle;
    private String readeJdbcTestOnBorrow;
    private String readeJdbcTestOnReturn;
    private String readeJdbcTimeBetweenEvictionRunsMillis;
    private String readeJdbcMinEvictableIdleTimeMillis;
    private String readeJdbcRemoveAbandoned;
    private String readeJdbcRemoveAbandonedTimeout;
    private String readeJdbcLogAbandoned;
    private String readeJdbcFilters;
    private String readeJdbcConnectionInitSqls;

    @Override
    public ReadWriteDataSource getObject() {
        return readWriteDataSource;
    }

    @Override
    public Class<?> getObjectType() {
        return ReadWriteDataSource.class;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Properties properties = new Properties();
        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field : fields) {
            if("pattern".equals(field.getName()) || field.get(this) == null) {
                continue;
            }
            properties.put(field.getName(), field.get(this));
        }
        this.initDatasource(properties);
    }

    private void initDatasource(Properties properties) throws Exception {
        this.destroy();
        ReadWriteDataSourceBuilder.createDataSource(this.readWriteDataSource, properties);
    }

    @Override
    public void destroy() throws Exception {
        ReadWriteDataSourceBuilder.destroy(readWriteDataSource);
    }

}
